﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MicrosoftOfficeCloud
{
    public partial class FrmOfficeCloud : Form
    {
        private CloudService SelectedService { get; set; }

        public FrmOfficeCloud()
        {
            InitializeComponent();

            pnlService.MouseEnter += pnlService_MouseEnter;
            lBServices.MouseEnter += lBServices_MouseEnter;

            btnAddDropbox.Visible = DropBoxProvider.IsInstalled() && !DropBoxProvider.IsRegistered();
            
            LoadServices();
        }

        private void LoadServices()
        {
            lBServices.Items.Clear();
            foreach (var cs in CloudService.GetServices())
                lBServices.Items.Add(cs);

            ClearCloudService();
        }

        void lBServices_MouseEnter(object sender, EventArgs e)
        {
            if(!pnlService.Controls.Cast<Control>().Contains(this.ActiveControl))
            {
                lBServices.Select();
                this.AcceptButton = btnAdd;
            }
        }

        void pnlService_MouseEnter(object sender, EventArgs e)
        {
            if(!pnlService.Controls.Cast<Control>().Contains(this.ActiveControl))
            {
                pnlService.Select();
            }

            this.AcceptButton = btnSave;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var cs = SelectedService ?? new CloudService();

            cs.DisplayName = txtDisplayName.Text;
            cs.Description = txtDescription.Text;
            cs.AccountUrl = txtAccountUrl.Text;
            cs.InformationUrl = txtInfoUrl.Text;
            cs.LocalFolder = txtLocalFolder.Text;
            cs.Url16x16 = txt16ImgUrl.Text;
            cs.Url20x20 = txt20ImgUrl.Text;
            cs.Url24x24 = txt24ImgUrl.Text;
            cs.Url32x32 = txt32ImgUrl.Text;
            cs.Url40x40 = txt40ImgUrl.Text;
            cs.Url48x48 = txt48ImgUrl.Text;

            try
            {
                cs.Save();

                LoadServices();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnBrowseLocalFolder_Click(object sender, EventArgs e)
        {
            var fbd = new FolderBrowserDialog { SelectedPath = txtLocalFolder.Text, Description = "Local folder" };

            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtLocalFolder.Text = fbd.SelectedPath;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            lBServices.SelectedIndex = -1;
        }

        private void lBServices_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lBServices.SelectedIndex != -1)
            {
                OpenCloudService(lBServices.SelectedItem as CloudService);
            }
            else
            {
                ClearCloudService();
            }
        }

        private void OpenCloudService(CloudService service)
        {
            SelectedService = service;

            txtDisplayName.Text = service.DisplayName;
            txtDescription.Text = service.Description;
            txtInfoUrl.Text = service.InformationUrl;
            txtAccountUrl.Text = service.AccountUrl;
            txt48ImgUrl.Text = service.Url48x48;
            txt40ImgUrl.Text = service.Url40x40;
            txt32ImgUrl.Text = service.Url32x32;
            txt24ImgUrl.Text = service.Url24x24;
            txt20ImgUrl.Text = service.Url20x20;
            txt16ImgUrl.Text = service.Url16x16;
            txtLocalFolder.Text = service.LocalFolder;
            btnRemove.Enabled = true;
        }

        private void ClearCloudService()
        {
            SelectedService = null;

            txtDisplayName.Text = "";
            txtDescription.Text = "";
            txtInfoUrl.Text = "";
            txtAccountUrl.Text = "";
            txt48ImgUrl.Text = "";
            txt40ImgUrl.Text = "";
            txt32ImgUrl.Text = "";
            txt24ImgUrl.Text = "";
            txt20ImgUrl.Text = "";
            txt16ImgUrl.Text = "";
            txtLocalFolder.Text = "";
            btnRemove.Enabled = false;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Do you want to remove \"" + SelectedService.DisplayName + "\"?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                SelectedService.Remove();

                LoadServices();
            }
        }

        private void btnAddDropbox_Click(object sender, EventArgs e)
        {
            new DropBoxProvider().Save();

            LoadServices();

            btnAddDropbox.Visible = false;

        }

    }
}
